package edu.pku.sei.modeler.editpolicies;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;

//import edu.pku.sei.modeler.actions.DeleteFromDiagramAction;
import edu.pku.sei.modeler.actions.DeleteFromModelAction;
import edu.pku.sei.modeler.actions.GraphNodeWithSemanticEditPolicy;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.uml.commands.UMLElementDeleteCommandFactory;
import edu.pku.sei.modeler.actions.DeleteFromDiagramAction;

public class UMLNodeEditPolicy extends GraphNodeWithSemanticEditPolicy {
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	public Command getCommand(Request request) {
		
		Object parent = this.getHost().getParent().getModel();
		if(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			if (parent instanceof MetaDiagram) {
				return getDeleteCommand((MetaNode)getTargetNode(), (MetaDiagram) parent, true);
			}
			else if (parent instanceof MetaNode){
				return getDeleteCommand((MetaNode)parent, getTargetNode(), true);
			}
			
			
		}
		if(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
			if (parent instanceof MetaDiagram) {
				return getDeleteCommand((MetaNode)getTargetNode(), (MetaDiagram)parent, false);
			}
			else if (parent instanceof MetaNode){
				return getDeleteCommand((MetaNode)parent, getTargetNode(), false);
			}
		}
		return null;
	}





    
    /**
     * FIXME bad smell
     * @param parentNode
     * @param targetNode
     * @return
     */
    @SuppressWarnings("static-access")
	protected Command getDeleteCommand(MetaNode parentNode, MetaShapeElement targetNode, boolean isDeleteFromDiagram) {
    	CompoundCommand command = new CompoundCommand();
		if((targetNode == null) || (parentNode == null)) {
			throw new IllegalArgumentException("child or parent is null ");
		}
		command.add(getDeleteCommandFactory().eINSTANCE.createDeleteCommand( (MetaNode)targetNode,parentNode,isDeleteFromDiagram, true));

		return command;
    }
	
	
	@SuppressWarnings("static-access")
	protected Command getDeleteCommand(MetaNode targetNode, MetaDiagram diagram, boolean isDeleteFromDiagram){

		CompoundCommand command = new CompoundCommand();
		if((targetNode == null) || (diagram == null)) {
			throw new IllegalArgumentException("child or parent is null ");
		}
		command.add(getDeleteCommandFactory().eINSTANCE.createDeleteCommand(targetNode, diagram, isDeleteFromDiagram, true));

		return command;
	}
	
		/* * Return the delete factory used to return a delete command.
	     * This method can be subclassed in order to return a specific factory.
	      * @return
	     */
	    protected UMLElementDeleteCommandFactory getDeleteCommandFactory() {
	        return UMLElementDeleteCommandFactory.eINSTANCE;
	    }
}
